/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

#include <time.h>

#include "sdcard.h"
#include "norflash.h"
/* Definitions of physical drive number for each drive */
#define DEV_FLASHEX		0
#define DEV_SDCARD		1
#define DEV_USB		2
#define SPI_FLASH_SECTOR_SIZE   512
#define SPI_FLASH_SECTOR_COUNT  12 * 1024 * 2
#define SPI_FLASH_BLOCK_SIZE    8
#define SPI_FLASH_FATFS_BASE    0
static volatile bool read_status,write_status;
/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{

	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{

	switch (pdrv) {
	case DEV_FLASHEX :
		norflash_init();
		return RES_OK;
	case DEV_SDCARD :
		sdcard_init();
		return RES_OK;

	// case DEV_USB :
	// 	result = USB_disk_initialize();
	//
	// 	// translate the reslut code here
	//
	// 	return stat;
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{

	switch (pdrv) {
	case DEV_SDCARD :
		if (sdcard_read_dma((uint8_t *)buff,sector,count)==HAL_OK)
			return RES_OK;
		return RES_ERROR;

	case DEV_FLASHEX :
		for (; count > 0; count--)
		{
			norflash_read(SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE,(uint8_t *)buff, SPI_FLASH_SECTOR_SIZE);
			sector++;
			buff += SPI_FLASH_SECTOR_SIZE;
		}
		return RES_OK;

	// case DEV_USB :
	// 	// translate the arguments here
	//
	// 	result = USB_disk_read(buff, sector, count);
	//
	// 	// translate the reslut code here
	//
	// 	return res;
	}

	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	DRESULT res;
	int result;

	switch (pdrv) {
	case DEV_FLASHEX :
		// translate the arguments here

		for (; count > 0; count--)
		{
			norflash_write(SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE,(uint8_t *)buff, SPI_FLASH_SECTOR_SIZE);
			sector++;
			buff += SPI_FLASH_SECTOR_SIZE;
		}
		return RES_OK;


	case DEV_SDCARD :
		// translate the arguments here
		if (sdcard_write_dma((uint8_t *)buff,sector,count)==HAL_OK)
			return RES_OK;
		return RES_ERROR;


	// case DEV_USB :
	// 	// translate the arguments here
	//
	// 	result = USB_disk_write(buff, sector, count);
	//
	// 	// translate the reslut code here
	//
	// 	return res;
	}

	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	int result;

	switch (pdrv) {
	case DEV_FLASHEX :
		switch (cmd)
		{
			case CTRL_SYNC:
					res = RES_OK;
					break;

			case GET_SECTOR_SIZE:
					*(WORD *)buff = SPI_FLASH_SECTOR_SIZE;
					res = RES_OK;
					break;

			case GET_BLOCK_SIZE:
					*(WORD *)buff = SPI_FLASH_BLOCK_SIZE;
					res = RES_OK;
					break;

			case GET_SECTOR_COUNT:
					*(DWORD *)buff = SPI_FLASH_SECTOR_COUNT;
					res = RES_OK;
					break;

			default:
					res = RES_PARERR;
					break;
		}
		// Process of the command for the RAM drive
		res=RES_OK;
		return res;

	case DEV_SDCARD :
		switch (cmd)
		{
			case CTRL_SYNC:
					res = RES_OK;
					break;

			case GET_SECTOR_SIZE:
					*(DWORD *)buff = 512;
					res = RES_OK;
					break;

			case GET_BLOCK_SIZE:
					*(WORD *)buff = g_sd_card_info.LogBlockSize;
					res = RES_OK;
					break;

			case GET_SECTOR_COUNT:
					*(DWORD *)buff = g_sd_card_info.LogBlockNbr;
					res = RES_OK;
					break;

			default:
					res = RES_PARERR;
					break;
		}
		// Process of the command for the MMC/SD card

		return res;

	case DEV_USB :

		// Process of the command the USB drive

		return res;
	}

	return RES_PARERR;
}


